package org.lex.io;

import java.io.IOException;
import java.io.Reader;

public class Line {
	private static final int CR = '\r';
	private static final int LF = '\n';
	private static final int EOF = -1;
	private int pushBackChar = EOF;
	private StringBuilder line = new StringBuilder(128);

	public StringBuilder readLine(Reader reader) throws IOException {
		line.setLength(0);
		if (EOF != pushBackChar) {
			line.append((char) pushBackChar);
			pushBackChar = EOF;
		}
		int ch = 0;
		while (true) {
			ch = reader.read();
			if (ch == CR || ch == LF || ch == EOF)
				break;
			line.append((char) ch);
		}
		ch = reader.read();
		if (ch != CR && ch != LF && ch != EOF)
			pushBackChar = ch;
		return line;
	}
}
